using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO;
namespace MicroMsg.Scene.Image
{
	public class UploadImgService
	{
		private const string TAG = "UploadImgService";
		private const int MAX_RUNNING = 5;
		private static TimerObject mTimerObject;
		public static ChatMsg startDoSence(string talkerName, string filename, Stream origStream, NetSceneUploadImage instance, int nImgMsgTranId, int msgType = 3, ParamEx param = null)
		{
			if (UpLoadImgContextMgr.getInstance().countRunningContext() >= 5)
			{
				Log.smethod_1("UploadImgService", "send Image scene cannot begin ,running task too much. ");
				return null;
			}
			if (talkerName == null || instance == null)
			{
				Log.smethod_1("UploadImgService", "one of the send Image scene cannot begin, param error.");
				return null;
			}
			if (origStream != null && filename != null)
			{
				UpLoadImgContext upLoadImgContext = new UpLoadImgContext();
				upLoadImgContext.talkerName = talkerName;
				upLoadImgContext.filename = filename;
				upLoadImgContext.origStream = origStream;
				upLoadImgContext.senceHandle = instance;
				upLoadImgContext.mMsgType = msgType;
				upLoadImgContext.mParamEx = param;
				Log.smethod_5("UploadImgService", "begin add chatmsg");
				if (upLoadImgContext.chatMsgInfo == null)
				{
					upLoadImgContext.chatMsgInfo = new ChatMsg();
				}
				upLoadImgContext.chatMsgInfo.strTalker = talkerName;
				upLoadImgContext.chatMsgInfo.nMsgType = msgType;
				upLoadImgContext.chatMsgInfo.nStatus = 0;
				upLoadImgContext.chatMsgInfo.nIsSender = 1;
				upLoadImgContext.chatMsgInfo.nCreateTime = (long)(Util.getNowMilliseconds() / 1000.0);
				upLoadImgContext.chatMsgInfo.strClientMsgId = MD5Core.GetHashString(talkerName + upLoadImgContext.GetHashCode() + Util.getNowMilliseconds());
				StorageMgr.chatMsg.addMsg(upLoadImgContext.chatMsgInfo);
				UpLoadImgContextMgr.getInstance().putToHead(upLoadImgContext);
				UploadImgService.checkReadyContextDispatcher();
				return upLoadImgContext.chatMsgInfo;
			}
			if (nImgMsgTranId <= 0)
			{
				Log.smethod_1("UploadImgService", "one of the send Image scene cannot begin, param error.");
				return null;
			}
			Log.smethod_4("UploadImgService", "Resend image nImgMsgTranId = " + nImgMsgTranId);
			UpLoadImgContext upLoadImgContext2 = new UpLoadImgContext();
			upLoadImgContext2.loadUploadImgContext(nImgMsgTranId);
			if (upLoadImgContext2.imgInfo != null && upLoadImgContext2.bigImageMemoryStream != null)
			{
				upLoadImgContext2.talkerName = talkerName;
				upLoadImgContext2.senceHandle = instance;
				UpLoadImgContextMgr.getInstance().putToHead(upLoadImgContext2);
				UploadImgService.checkReadyContextDispatcher();
				return upLoadImgContext2.chatMsgInfo;
			}
			Log.smethod_1("UploadImgService", "one of the send Image scene cannot begin, loadUploadImgContext from db error.");
			return null;
		}
		private static void checkReadyContextDispatcher()
		{
			if (UploadImgService.mTimerObject == null)
			{
				UploadImgService.mTimerObject = TimerService.addTimer(8, new EventHandler(UploadImgService.onImageContextDispatcher), 1, -1);
				UploadImgService.mTimerObject.start();
			}
		}
		public static void onImageContextDispatcher(object sender, EventArgs e)
		{
			UpLoadImgContextMgr.getInstance().clearnFinishedContext();
			if (UpLoadImgContextMgr.getInstance().countRunningContext() >= 5)
			{
				return;
			}
			UpLoadImgContext firstContextNeedHandle = UpLoadImgContextMgr.getInstance().getFirstContextNeedHandle();
			if (firstContextNeedHandle == null)
			{
				Log.smethod_4("UploadImgService", "No more image need send. ");
				if (UpLoadImgContextMgr.getInstance().countRunningContext() == 0)
				{
					List<ChatMsg> list = StorageMgr.chatMsg.searhCacheMsg(3, 0, 1);
					if (list != null && list.get_Count() > 0)
					{
						using (List<ChatMsg>.Enumerator enumerator = list.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								ChatMsg current = enumerator.get_Current();
								current.nStatus = 1;
							}
						}
						StorageMgr.chatMsg.updateMsgList(list);
					}
					Log.smethod_4("UploadImgService", "all ready, close dispatcher timer. ");
					UploadImgService.mTimerObject.stop();
					UploadImgService.mTimerObject = null;
				}
				return;
			}
			if (firstContextNeedHandle.imgInfo == null && firstContextNeedHandle.origStream != null && !firstContextNeedHandle.fillContextWithOrigStream())
			{
				Log.smethod_1("UploadImgService", "fillContext fail!");
				firstContextNeedHandle.mStatus = 5;
				return;
			}
			if (firstContextNeedHandle.senceHandle == null)
			{
				firstContextNeedHandle.senceHandle = new NetSceneUploadImage();
			}
			firstContextNeedHandle.senceHandle.mOnSceneFinished += new onSceneFinishedDelegate(UploadImgService.onSceneFinished);
			firstContextNeedHandle.senceHandle.doScene(firstContextNeedHandle);
		}
		private static void onSceneFinished(UpLoadImgContext context)
		{
		}
	}
}
